package com.pda.yuanjing.pages.custombill.scanf;

import android.content.Context;
import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.beanin.DownBaseBeanIn;
import com.pda.yuanjing.beanin.JieDianBeanIn;
import com.pda.yuanjing.beanout.BaseBeanOut;
import com.pda.yuanjing.beanout.DealerBeanOut;
import com.pda.yuanjing.beanout.JDBeanDNewOut;
import com.pda.yuanjing.beanout.JDBeanNewOut;
import com.pda.yuanjing.beanout.JDBeanOut;
import com.pda.yuanjing.beanout.JDListBeanOut;
import com.pda.yuanjing.beanout.NoBillBeanOut;
import com.pda.yuanjing.beanout.ProductBeanOut;
import com.pda.yuanjing.beanout.ScanfBeanOut;
import com.pda.yuanjing.beanout.StoreBeanOut;
import com.pda.yuanjing.thread.CodeBeans;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class ScanfModelImpl extends BaseModel implements ScanfModel {
    private String TAG = "InScanfModelImpl";

    public ScanfModelImpl(Context context) {
        super(context);
    }

    @Override
    public void scanfCodenobill(String  billCode, String processID, String jieDianName, String codes, String selectMsg, String selectMsg1, String sid, String pid, String did, CodeBeans codeBeans, String s1,String s2,String s3,CallBack callBack)
    {
        NoBillBeanOut jdBeanOut = new NoBillBeanOut();
        jdBeanOut.setErrcode(-1);
        jdBeanOut.setErrmsg(SetConfig.getConnectError());
        try {
            JieDianBeanIn jieDianBeanIn = new JieDianBeanIn();
            jieDianBeanIn.setAction("SY_UpdateCodeNoBill");
            jieDianBeanIn.setCid(SharedPreferencesManger.getCid());
            jieDianBeanIn.setUserID(SharedPreferencesManger.getUid());
            jieDianBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            jieDianBeanIn.setBillCode(billCode);
            jieDianBeanIn.setProcessID(Integer.parseInt(processID));
            jieDianBeanIn.setJieDianName(jieDianName);
            jieDianBeanIn.setCode(codes);
            jieDianBeanIn.setSelectMsg(selectMsg);
            jieDianBeanIn.setSelectMsgTxt(selectMsg1);
            jieDianBeanIn.setStoreIDIn(sid);
            jieDianBeanIn.setProductID(pid);
            jieDianBeanIn.setDealerID(did);
            jieDianBeanIn.setSelectMsgTXPZ1(s1);
            jieDianBeanIn.setSelectMsgTXPZ2(s2);
            jieDianBeanIn.setTxbzMsg(s3);
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(jieDianBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                jdBeanOut = gson.fromJson(StringProcess.responsetoString(response.body()), NoBillBeanOut.class);
                jdBeanOut.getData().setCode(jdBeanOut.getErrcode());
                jdBeanOut.getData().setStr(jdBeanOut.getErrmsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            jdBeanOut.setErrcode(-1);
            jdBeanOut.setErrmsg(SetConfig.getConnectError());
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            callBack.onshowSuccessresNoBill(jdBeanOut.getData());
//            if (jdBeanOut.getErrcode() == 0) {
//                callBack.onshowSuccessresNoBill(jdBeanOut.getData());
//            } else {
//                callBack.onshowres(jdBeanOut.getErrmsg());
//            }
        }
    }

    @Override
    public void scanfCode2(String batchID, String  billListID,  String codes,String selectMsg, CodeBeans codeBeans, CallBack callBack)
    {
        JDListBeanOut jdListBeanOut = new JDListBeanOut();
        jdListBeanOut.setErrcode(-1);
        jdListBeanOut.setErrmsg(SetConfig.getConnectError());
        try {
            JieDianBeanIn jieDianBeanIn = new JieDianBeanIn();
            jieDianBeanIn.setAction("SY_UpdateCode");
            jieDianBeanIn.setCid(SharedPreferencesManger.getCid());
            jieDianBeanIn.setUserID(SharedPreferencesManger.getUid());
            jieDianBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            jieDianBeanIn.setBatchID(batchID);
            jieDianBeanIn.setBillListID(billListID);
            jieDianBeanIn.setCodes(codes);
            jieDianBeanIn.setSelectMsg(selectMsg);
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(jieDianBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                jdListBeanOut = gson.fromJson(StringProcess.responsetoString(response.body()), JDListBeanOut.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jdListBeanOut.setErrcode(-1);
            jdListBeanOut.setErrmsg(SetConfig.getConnectError());
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            if (jdListBeanOut.getErrcode() == 0) {
                callBack.onshowSuccessres(jdListBeanOut);
            } else {
                callBack.onshowres(jdListBeanOut.getErrmsg());
            }
        }
    }



    @Override
    public  int downLoadInfo1(String processID,String jieDianName) {
        try {
            PDApp.productBeanOutt.setErrcode(-1);
            PDApp.productBeanOutt.setData(null);
            DownBaseBeanIn downBaseBeanIn = new DownBaseBeanIn();
            downBaseBeanIn.setAction("DownloadBaseInfo");
            downBaseBeanIn.setCid(SharedPreferencesManger.getCid());
            downBaseBeanIn.setUserID(SharedPreferencesManger.getUid());
            downBaseBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            downBaseBeanIn.setProcessID(processID);
            downBaseBeanIn.setJieDianName(jieDianName);
            downBaseBeanIn.setBaseType("1");
            downBaseBeanIn.setBillType("-1");
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(downBaseBeanIn));
            final Call<ResponseBody> call = apisync.downLoad(body);
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response<ResponseBody> response = call.execute();
                        if (response.isSuccessful()) {
                            LogUtils.e(StringProcess.responsetoString(response.body()));
                            PDApp.productBeanOutt = gson.fromJson(StringProcess.responsetoString(response.body()), ProductBeanOut.class);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return PDApp.productBeanOutt.getErrcode();
        }
    }

    @Override
    public int downLoadInfo2(String processID,String jieDianName) {
        try {
            PDApp.storeBeanOutt.setErrcode(-1);
            PDApp.storeBeanOutt.setData(null);
            DownBaseBeanIn downBaseBeanIn = new DownBaseBeanIn();
            downBaseBeanIn.setAction("DownloadBaseInfo");
            downBaseBeanIn.setCid(SharedPreferencesManger.getCid());
            downBaseBeanIn.setUserID(SharedPreferencesManger.getUid());
            downBaseBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            downBaseBeanIn.setProcessID(processID);
            downBaseBeanIn.setJieDianName(jieDianName);
            downBaseBeanIn.setBaseType("2");
            downBaseBeanIn.setBillType("-1");
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(downBaseBeanIn));
            final Call<ResponseBody> call = apisync.downLoad(body);
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response<ResponseBody> response = call.execute();
                        if (response.isSuccessful()) {
                            LogUtils.e(StringProcess.responsetoString(response.body()));
                            PDApp.storeBeanOutt = gson.fromJson(StringProcess.responsetoString(response.body()), StoreBeanOut.class);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return PDApp.storeBeanOutt.getErrcode();
        }
    }

    @Override
    public int downLoadInfo3(String processID,String jieDianName) {
        try {
            PDApp.dealerBeanOutt.setErrcode(-1);
            PDApp.dealerBeanOutt.setData(null);
            DownBaseBeanIn downBaseBeanIn = new DownBaseBeanIn();
            downBaseBeanIn.setAction("DownloadBaseInfo");
            downBaseBeanIn.setCid(SharedPreferencesManger.getCid());
            downBaseBeanIn.setUserID(SharedPreferencesManger.getUid());
            downBaseBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            downBaseBeanIn.setProcessID(processID);
            downBaseBeanIn.setJieDianName(jieDianName);
            downBaseBeanIn.setBaseType("3");
            downBaseBeanIn.setBillType("-1");

            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(downBaseBeanIn));
            final Call<ResponseBody> call = apisync.downLoad(body);
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response<ResponseBody> response = call.execute();
                        if (response.isSuccessful()) {
                            LogUtils.e(StringProcess.responsetoString(response.body()));
                            PDApp.dealerBeanOutt = gson.fromJson(StringProcess.responsetoString(response.body()), DealerBeanOut.class);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return PDApp.dealerBeanOutt.getErrcode();
        }
    }
}

